package info.openlvb.openlvg.server.rrd;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.GregorianCalendar;

public class CreateImg {
	Process process = null;
	public CreateImg() {

	}
	
	public boolean createImg(String[] arg){
		int result=1;
		String RRDPATH = "/var/lib/rrdcollect";
		String WARIMAGES = "/home/deletex/workspace/openface/war/images";
		String type = arg[0];

		/*TODO
		 * - Prise en compte de la date et non seulement des heures ;
		 * - Vérification des valeurs (cohérence) ;
		 */
		String startDay = arg[1];
		String endDay = arg[2];
		String startTime = arg[3];
		String endTime = arg[4];
		
		String rrdStartDay = startDay.substring(3,6)+startDay.substring(0,3)+startDay.substring(6,8);
		String rrdEndDay = endDay.substring(3,6)+endDay.substring(0,3)+endDay.substring(6,8);

		String cmdRRD1 ="rrdtool";
		String cmdRRD2 ="graph";
		String nameCpu = WARIMAGES+"/cpu.png";
		String nameMem = WARIMAGES+"/mem.png";
		String nameNet = WARIMAGES+"/net.png";
		String cmd[] = null;
		String file = null;
		
		String options[] ={"-s",""+rrdStartDay+" "+startTime,"-e",""+rrdEndDay+" "+endTime};
		String[] optionsCpu ={"--upper-limit","100","--lower-limit","0"};
		String cpuImg[] ={"DEF:cpu_user="+RRDPATH+"/stat.rrd:cpu_user:AVERAGE","DEF:cpu_nice="+RRDPATH+"/stat.rrd:cpu_nice:AVERAGE","DEF:cpu_system="+RRDPATH+"/stat.rrd:cpu_system:AVERAGE","DEF:cpu_idle="+RRDPATH+"/stat.rrd:cpu_idle:AVERAGE","AREA:cpu_user#0000FF:USER","STACK:cpu_system#FF9999:system","STACK:cpu_nice#FF99FF:nice","STACK:cpu_user#99FF99:user","STACK:cpu_idle#FFFFFF:idle"};
		String[] optionsMem ={"--upper-limit","100","--lower-limit","0"};
		String[] memImg = {"DEF:mem_total="+RRDPATH+"/memory.rrd:mem_total:AVERAGE","DEF:mem_free="+RRDPATH+"/memory.rrd:mem_free:AVERAGE","CDEF:mempcent=mem_free,mem_total,/,100,*","LINE:mempcent#0000FF:Pourcentage"};
		String optionsNet[] ={"--base", "1024", "--lower-limit", "-25000", "--upper-limit", "25000"};
		String netImg[]={"DEF:lo_Rbytes="+RRDPATH+"/net.rrd:lo_Rbytes:AVERAGE","DEF:lo_Rpackets="+RRDPATH+"/net.rrd:lo_Rpackets:AVERAGE","DEF:lo_Rerrs="+RRDPATH+"/net.rrd:lo_Rerrs:AVERAGE","DEF:lo_Tbytes="+RRDPATH+"/net.rrd:lo_Tbytes:AVERAGE","DEF:lo_Tpackets="+RRDPATH+"/net.rrd:lo_Tpackets:AVERAGE","DEF:lo_Terrs="+RRDPATH+"/net.rrd:lo_Terrs:AVERAGE","DEF:eth0_Rbytes="+RRDPATH+"/net.rrd:eth0_Rbytes:AVERAGE","DEF:eth0_Rpackets="+RRDPATH+"/net.rrd:eth0_Rpackets:AVERAGE","DEF:eth0_Rerrs="+RRDPATH+"/net.rrd:eth0_Rerrs:AVERAGE","DEF:eth0_Tbytes="+RRDPATH+"/net.rrd:eth0_Tbytes:AVERAGE","DEF:eth0_Tpackets="+RRDPATH+"/net.rrd:eth0_Tpackets:AVERAGE","DEF:eth0_Terrs="+RRDPATH+"/net.rrd:eth0_Terrs:AVERAGE","CDEF:lo_RXbytes=lo_Rbytes,-1,*,1024,/","CDEF:lo_RXpackets=lo_Rpackets,-1,*","CDEF:lo_RXerrs=lo_Rerrs,-1,*","CDEF:lo_TXbytes=lo_Tbytes,1024,/","CDEF:eth0_RXbytes=eth0_Rbytes,-1,*,1024,/","CDEF:eth0_RXpackets=eth0_Rpackets,-1,*","CDEF:eth0_RXerrs=eth0_Rerrs,-1,*","AREA:lo_Tpackets#443355:lo_Tpackets","AREA:lo_RXpackets#00FF00:lo_Rpackets","AREA:lo_TXbytes#F155F1:lo_Tbytes","AREA:lo_RXbytes#0000FF:lo_Rbytes"};

		if (type.equals("cpu")){
			String titre ="'Charge CPU entre le "+startDay+" à "+startTime+" et le "+endDay+" à "+endTime+"'";
			String title[] ={"--title",titre};
			file = nameCpu;
			cmd=new String[3+title.length+options.length+cpuImg.length];
			cmd[0]=cmdRRD1;
			cmd[1]=cmdRRD2;
			cmd[2]=file;
			for(int i=0;i<title.length;i++){
				cmd[i+3]=title[i];
			}
			for(int i=0;i<options.length;i++){
				cmd[i+3+title.length]=options[i];
			}
			for(int i=0;i<optionsCpu.length;i++){
				cmd[i+3+title.length+options.length]=optionsCpu[i];
			}
			for(int i=0;i<cpuImg.length;i++){
				cmd[i+3+title.length+options.length]=cpuImg[i];
			}
		}
		else if (type.equals("mem")){
			String titre ="'Utilisation de la mémoire entre le "+startDay+" à "+startTime+" et le "+endDay+" à "+endTime+"'";
			String title[] ={"--title",titre};
			file = nameMem;
			cmd=new String[3+title.length+optionsMem.length+memImg.length+options.length];
			cmd[0]=cmdRRD1;
			cmd[1]=cmdRRD2;
			cmd[2]=file;
			for(int i=0;i<title.length;i++){
				cmd[i+3]=title[i];
			}
			for(int i=0;i<options.length;i++){
				cmd[i+3+title.length]=options[i];
			}
			for(int i=0;i<optionsMem.length;i++){
					cmd[i+3+title.length+options.length]=optionsMem[i];
			}
			for(int i=0;i<memImg.length;i++){
					cmd[i+3+title.length+options.length+optionsMem.length]=memImg[i];
			}
		}
		else if (type.equals("net")){
			String titre ="'Charge réseau entre le "+startDay+" à "+startTime+" et le "+endDay+" à "+endTime+"'";
			String title[] ={"--title",titre};
			file = nameNet;
			cmd=new String[3+title.length+options.length+optionsNet.length+netImg.length];
			cmd[0]=cmdRRD1;
			cmd[1]=cmdRRD2;
			cmd[2]=file;
			for(int i=0;i<title.length;i++){
				cmd[i+3]=title[i];
			}
			for(int i=0;i<options.length;i++){
				cmd[i+3+title.length]=options[i];
			}
			for(int i=0;i<optionsNet.length;i++){
				cmd[i+3+title.length+options.length]=optionsNet[i];
			}
			for(int i=0;i<netImg.length;i++){
				cmd[i+3+title.length+options.length+optionsNet.length]=netImg[i];
			}
			//img = netImg;
		}
		else return false;
		
		//visu cmd:
		for(int i=0;i<cmd.length;i++){
			System.out.print(cmd[i]+" ");
		}
		System.out.println();
		System.out.println("Date de début : "+startDay);
		Runtime runtime = Runtime.getRuntime();
		try
		{
			process = runtime.exec(cmd);
			result = process.waitFor();
		} catch(Exception err) {
			System.out.println(err.toString()+process.getOutputStream().toString());
			}
		// Consommation de la sortie standard de l'application externe dans un Thread separe
		new Thread() {
			public void run() {
				try {
					BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
					String line = "";
					try {
						while((line = reader.readLine()) != null) {
							// Traitement du flux de sortie de l'application si besoin est
						}
					} finally {
						reader.close();
					}
				} catch(IOException ioe) {
					ioe.printStackTrace();
				}
			}
		}.start();

		// Consommation de la sortie d'erreur de l'application externe dans un Thread separe
		new Thread() {
			public void run() {
				try {
					BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
					String line = "";
					try {
						while((line = reader.readLine()) != null) {
							// Traitement du flux d'erreur de l'application si besoin est
							System.out.println(line);
						}
					} finally {
						reader.close();
					}
				} catch(IOException ioe) {
					ioe.printStackTrace();
				}
			}
		}.start();

		return result==0;
	}
}